package cn.chia.back.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import cn.chia.pojo.Orders;
import cn.chia.pojo.Ordertime;
import cn.chia.pojo.Peisongren;
import cn.chia.pojo.TblUser;
import cn.chia.service.OrderService;
import cn.chia.service.OrdertimeService;
import cn.chia.service.SenderService;
import cn.chia.service.StorekeeperService;
import cn.chia.utils.BuildLogFile;
import cn.chia.utils.LogUtils;
import cn.chia.utils.Md5Util;
import cn.chia.utils.Onstorekeeper;

@Controller
public class StorekeeperAction {
	private StorekeeperService storekeeperService;
	private OrderService orderService;            //订单Service
	private SenderService senderService;
	private Orders order;
	private TblUser tbluser;
	private int id;
	private int state;
	private int inmanage; 
	private List<?> list;
	private String verifycode;
	private String userName;
	private String password;
	private boolean cstixing; 
	private int senderid;
	private Set set;
	private List<?> senderlist;
	private OrdertimeService ordertimeService;
	private int flagg;
	
	public String login() throws Exception{
		System.out.println("ppppppppppppppppppppp");
		HttpServletRequest request = ServletActionContext.getRequest();
		//验证码验证
		String scode = (String) request.getSession().getAttribute("VerifyCode");
		if(!verifycode.equalsIgnoreCase(scode)){
			request.setAttribute("error2", "验证码错误!!");
			return "error";
		}
		System.out.println(userName+password+"99999999999");
		tbluser = storekeeperService.findByUserPass(userName,Md5Util.addMd5(password));
		System.out.println(Md5Util.addMd5("123")+"???????????????????????/");
		System.out.println(tbluser+"/////////////////////////");
		if(tbluser==null){
			request.setAttribute("error","用户名或密码错误!");
			return "error";
		}else if(tbluser.getTblRole().getRoleId()!=2){
			request.setAttribute("error","您不是仓管员，不能登录!");
			return "error";
		}
		ServletActionContext.getRequest().getSession().setAttribute("onstorekeeper",tbluser);
		//生成日志
		LogUtils.addInfo(Onstorekeeper.storekeeper().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"登陆本系统.\r\n");
		return "success";
	}
	
	public String logout(){
		//生成日志
		BuildLogFile.build(Onstorekeeper.storekeeper().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"退出本系统.\r\n");
		ServletActionContext.getRequest().getSession().removeAttribute("onstorekeeper");
		return "success";
	}
	
	public String listbystate() throws Exception{
		list = orderService.findOrdersByStateBack("已付款");
		return "success";
	}
	
	public String listbystate1() throws Exception{
		list = orderService.findOrdersByStateBack("配送中");
		return "success";
	}
	
	public String updateInmanage() throws Exception{
		order = orderService.findById(id);
		//inmanage为1时正在被处理
			if(inmanage == 1){
				//判断当前客服和正在处理客服是否为同一人
				if(order.getStorekeeper().getUserId().equals(Onstorekeeper.storekeeper().getUserId())){
					order.setInmanage(0);
					order.setStorekeeper(null);
					orderService.update(order);
					cstixing = false;
				}else{
					cstixing = true;
				}
			//否则未被处理或处理完成
			}else{
				order.setInmanage(1);
				order.setStorekeeper(Onstorekeeper.storekeeper());
				orderService.update(order);
			}
			if(flagg==1){list = orderService.findordersbystateuszdtime("已付款", "立即发货");}
			if(flagg==0){list = orderService.findOrdersByStateBack("已付款");}
		return "success";
	}
	
	public String viewbyid() throws Exception{
		//某一条订单
		order = orderService.findById(id);
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@-------收货人姓名："+order.getReceivername());
		//将这条订单的商品明细放到set集合中
		set = order.getOrderitemses();
		//查找所有的送货人
		senderlist = senderService.findall();
		return "success";
	}
	
	public String updateorderstate() throws Exception{
		//发货后的操作，5表示已发货
		//order = orderService.findById(id);
		if(state==5){
			order.setState("已发货");
			//设置发货的配送人
			Peisongren peisongren = new Peisongren();
			peisongren.setId(senderid);
			order.setPeisongren(peisongren);
			//修改订单时间
			Ordertime ot = ordertimeService.findById(order.getOrdertime().getOrdernumber());
			ot.setSendtime(new Date());
			ordertimeService.update(ot);
			//表示此订单正在被处理中
			order.setInmanage(0);
		}
		//生成日志
		BuildLogFile.build(Onstorekeeper.storekeeper().getUserName() + "于" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "向收货人" + order.getReceivername() + "发货，订单号为: " + order.getOrderNumber() + "。\r\n");
		orderService.updateFront(order);
		order.setState("配送中");	
		orderService.updateFront(order);
		return "success";
	}
	
	public String findordersbystateuszdtime() throws Exception{
		list = orderService.findordersbystateuszdtime("已付款", "立即发货");
		return "success";
	}
	
	public Orders getOrder() {
		return order;
	}
	public void setOrder(Orders order) {
		this.order = order;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public List<?> getList() {
		return list;
	}
	public void setList(List<?> list) {
		this.list = list;
	}
	public int getState() {
		return state;
	}



	public void setState(int state) {
		this.state = state;
	}



	public String getVerifycode() {
		return verifycode;
	}

	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public TblUser getTbluser() {
		return tbluser;
	}

	public void setTbluser(TblUser tbluser) {
		this.tbluser = tbluser;
	}
	public int getInmanage() {
		return inmanage;
	}

	public void setInmanage(int inmanage) {
		this.inmanage = inmanage;
	}

	public boolean isCstixing() {
		return cstixing;
	}

	public Set getSet() {
		return set;
	}

	public void setSet(Set set) {
		this.set = set;
	}

	public void setCstixing(boolean cstixing) {
		this.cstixing = cstixing;
	}
	
	public List<?> getSenderlist() {
		return senderlist;
	}

	public void setSenderlist(List<?> senderlist) {
		this.senderlist = senderlist;
	}

	public int getSenderid() {
		return senderid;
	}

	public void setSenderid(int senderid) {
		this.senderid = senderid;
	}


	public int getFlagg() {
		return flagg;
	}

	public void setFlagg(int flagg) {
		this.flagg = flagg;
	}

	@Resource(name="ordertimeServiceImpl")
	public void setOrdertimeService(OrdertimeService ordertimeService) {
		this.ordertimeService = ordertimeService;
	}

	@Resource(name="senderServiceImpl")
	public void setSenderService(SenderService senderService) {
		this.senderService = senderService;
	}

	@Resource(name="storekeeperServiceImpl")
	public void setStorekeeperService(StorekeeperService storekeeperService) {
		this.storekeeperService = storekeeperService;
	}

	@Resource(name="orderServiceImpl")
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
}
